# get the zynga host and referer for the http requests.
require "00_common"


ZYNGA_INPUT = "./zynga_payload/"
ZYNGAANDROID_OUTPUT = "./zynga_android/"

module ZyngaAndroid
   def self.run
    puts ">> start to get zynga android records..."

    begin
      all_files = ZyngaAndroid.get_all_files
      puts all_files
      ZyngaAndroid.handle_all_files all_files

      puts ">> end to get zynga android records."
    rescue Exception => e
      puts ">> EEROR:"
      puts e.to_s
    end
  end



  def self.get_all_files
  	Common.get_all_files(ZYNGA_INPUT)
  end


    def self.handle_each_file(file_name)

    puts "now handling : " + file_name
    file = File.open ZYNGA_INPUT + file_name, "r"
    output_file_name = Common.alternate_file_name(file_name, "_payload_", "_android_payload_")
    output_file = File.open output_file_name, "w"

    begin
      counter = 0
      while line = file.readline
        counter = counter + 1

        line = line.rstrip
        if line.end_with? "|"
          line = line + "NULL"
        end

        fields = line.split "|"

        local_time_stamp    = Common.check_string fields[0]
        session_id          = Common.check_string fields[1]
        session_start_time  = Common.check_string fields[2]
        server_ip           = Common.check_string fields[3]
        server_port         = Common.check_string fields[4]
        client_ip           = Common.check_string fields[5]
        client_port         = Common.check_string fields[6]
        referer             = Common.check_string fields[7]
        host                = Common.check_string fields[8]
        content_type        = Common.check_string fields[9]
        content_length      = Common.check_string fields[10]
        user_agent          = Common.check_string fields[11]
        connection          = Common.check_string fields[12]
        c_request           = Common.check_string fields[13]
        s_response          = Common.check_string fields[14]
        cookie              = Common.check_string fields[15]


        if (user_agent.include?  "android")
         new_line = "#{local_time_stamp}|#{session_id}|#{session_start_time}|#{server_ip}|#{server_port}|#{client_ip}|#{client_port}"
         new_line = new_line + "|#{referer}|#{host}|#{content_type}|#{content_length}|#{user_agent}|#{connection}|#{c_request}|#{s_response}|#{cookie}"

          output_file.puts new_line
        end # of if
        if counter % 100000 == 0
          puts counter
        end
      end
    rescue Exception => e
      puts e.to_s
      file.close
    end

    output_file.close
  end



  def self.handle_all_files(all_files)
  	all_files.each do |file_name|
    	if Common.file_exist?(ZYNGAANDROID_OUTPUT, Common.alternate_file_name(file_name, "_payload_", "_android_payload_"))
    	  puts "exist file:" + file_name
    	else
        handle_each_file(file_name)
        Common.move_file Common.alternate_file_name(file_name, "_payload_","_android_payload_"), ZYNGAANDROID_OUTPUT
    	end
    end
  end
end


